package com.neu.fenli2023623.service.impl;

import com.neu.fenli2023623.dto.UserDto;
import com.neu.fenli2023623.entity.User;
import com.neu.fenli2023623.mapper.UserMapper;
import com.neu.fenli2023623.service.UserService;
import com.neu.fenli2023623.vo.RightVo;
import com.neu.fenli2023623.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User Login(UserDto user) {
        return userMapper.Login(user);
    }

    @Override
    public List<RightVo> selectRightByAccount(String account, Integer parentId) {
        Set<Integer> visited = new HashSet<>();
        return selectRightByAccount(account, parentId, visited);
    }
    public List<RightVo> selectRightByAccount(String account, Integer parentId, Set<Integer> visited) {
        List<RightVo> rightVos = userMapper.selectRightByAccount(account, parentId);
        if (rightVos != null) {
            for (RightVo rightVo : rightVos) {
                if (!visited.contains(rightVo.getId())) {
                    visited.add(rightVo.getId());
                    List<RightVo> children = selectRightByAccount(account, rightVo.getId(), visited);
                    rightVo.setChildren(children);
                }
            }
        }
        return rightVos;
    }


    @Override
    public List<UserVo> selectAll(UserDto userDto) {
        return userMapper.selectAll(userDto);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public int del(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User selectByAccount(String account) {
        return userMapper.selectByAccount(account);
    }

    @Override
    public String[] selectPermissionByAccount(String account) {
        return userMapper.selectPermissionByAccount(account);
    }

    @Override
    public String[] selectRoleByAccount(String account) {
        return userMapper.selectRoleByAccount(account);
    }

    @Override
    public List<UserVo> selectExportData(UserDto userDto) {
        return userMapper.selectExportData(userDto);
    }
}
